home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Managed / Direct3D / PrtPerVertex / Skybox.cs < prev    next >
Encoding:
Text File  |  2004-09-27  |  7.5 KB  |  197 lines

  1. using System;
  2. using System.Runtime.InteropServices;
  3.  
  4. using Microsoft.DirectX;
  5. using Microsoft.DirectX.Direct3D;
  6. using Microsoft.Samples.DirectX.UtilityToolkit;
  7.  
  8. namespace PrtPerVertexSample
  9. {
  10.     /// <summary>
  11.     /// Encapsulation of skybox geometry and textures.
  12.     /// </summary>
  13.     public class Skybox
  14.     {
  15.         public struct SkyboxVertexFormat
  16.         {
  17.             public Vector4 Position;
  18.             public Vector3 Texture;
  19.         };
  20.  
  21.         #region Constants
  22.         private VertexElement[] skyboxDeclaration =
  23.         {
  24.             new VertexElement(0, 0,  DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.Position, 0),
  25.             VertexElement.VertexDeclarationEnd
  26.         };
  27.         #endregion
  28.  
  29.         #region Instance Data
  30.         private CubeTexture environmentMap = null;
  31.         private CubeTexture environmentMapSH = null;
  32.         private Effect effect = null;
  33.         private VertexBuffer vertexBuffer = null;
  34.         private VertexDeclaration vertexDeclaration = null;
  35.         private Device device = null;   
  36.         private float size = 1.0f;
  37.         private bool drawSH = false;
  38.         #endregion
  39.  
  40.         #region Properties
  41.         public CubeTexture EnvironmentMap { get { return environmentMap; } }
  42.         public bool DrawSH { get { return drawSH; } set { drawSH = value; } }
  43.         #endregion
  44.  
  45.         public void OnCreateDevice( Device device, float size, CubeTexture environmentMap, string effectFileName )
  46.         {
  47.             this.device = device;
  48.             this.size = size;
  49.             this.environmentMap = environmentMap;
  50.  
  51.             // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the shader debugger.  
  52.             // Debugging vertex shaders requires either REF or software vertex processing, and debugging 
  53.             // pixel shaders requires REF.  The "ShaderFlags.Force[Pixel/Vertex]ShaderSoftwareNoOptimizations" flag improves the debug 
  54.             // experience in the shader debugger.  It enables source level debugging, prevents instruction 
  55.             // reordering, prevents dead code elimination, and forces the compiler to compile against the next 
  56.             // higher available software target, which ensures that the unoptimized shaders do not exceed 
  57.             // the shader model limitations.  Setting these flags will cause slower rendering since the shaders 
  58.             // will be unoptimized and forced into software.  See the DirectX documentation for more information 
  59.             // about using the shader debugger.
  60.             ShaderFlags shaderFlags = ShaderFlags.None;
  61.             #if(DEBUG_VS)
  62.                 shaderFlags |= ShaderFlags.ForceVertexShaderSoftwareNoOptimizations;
  63.             #endif
  64.             #if(DEBUG_PS)
  65.                 shaderFlags |= ShaderFlags.ForcePixelShaderSoftwareNoOptimizations;
  66.             #endif
  67.  
  68.             // Read the D3DX effect file
  69.             string fileNameWithPath = Utility.FindMediaFile(effectFileName);
  70.  
  71.             // If this fails, there should be debug output as to 
  72.             // they the .fx file failed to compile
  73.             effect = Effect.FromFile(device, fileNameWithPath , null, shaderFlags, null);
  74.  
  75.             // Create vertex declaration
  76.             vertexDeclaration = new VertexDeclaration(device, skyboxDeclaration);
  77.         }
  78.  
  79.         public void InitSH(CubeTexture shTexture)
  80.         {
  81.             environmentMapSH = shTexture;
  82.         }
  83.  
  84.         public void OnCreateDevice( Device device, float size, string cubeMapFileName, string effectFileName )
  85.         {
  86.             string mediaFileLongName = Utility.FindMediaFile(cubeMapFileName);
  87.             
  88.             CubeTexture environmentMap = ResourceCache.GetGlobalInstance().CreateCubeTextureFromFileEx(device, mediaFileLongName, 
  89.                 D3DX.Default, 1, Usage.None, Format.A16B16G16R16F, Pool.Managed, Filter.None, Filter.None, 0);
  90.  
  91.             OnCreateDevice( device, size, environmentMap, effectFileName );
  92.         }
  93.  
  94.         public void OnResetDevice( SurfaceDescription backBufferSurfaceDesc )
  95.         {
  96.             if( effect != null )
  97.                 effect.OnResetDevice();
  98.  
  99.             vertexBuffer = new VertexBuffer(typeof(SkyboxVertexFormat), 4, device, Usage.WriteOnly, VertexFormats.None, Pool.Default);
  100.             // Fill the vertex buffer
  101.             SkyboxVertexFormat[] vertex = (SkyboxVertexFormat[]) vertexBuffer.Lock(0, LockFlags.None);
  102.             try
  103.             {
  104.                 // Map texels to pixels 
  105.                 float highW = -1.0f - (1.0f / (float)backBufferSurfaceDesc.Width);
  106.                 float highH = -1.0f - (1.0f / (float)backBufferSurfaceDesc.Height);
  107.                 float lowW  =  1.0f + (1.0f / (float)backBufferSurfaceDesc.Width);
  108.                 float lowH  =  1.0f + (1.0f / (float)backBufferSurfaceDesc.Height);
  109.  
  110.                 vertex[0].Position = new Vector4(lowW,  lowH,  1.0f, 1.0f);
  111.                 vertex[1].Position = new Vector4(lowW,  highH, 1.0f, 1.0f);
  112.                 vertex[2].Position = new Vector4(highW, lowH,  1.0f, 1.0f);
  113.                 vertex[3].Position = new Vector4(highW, highH, 1.0f, 1.0f);
  114.             }
  115.             finally
  116.             {
  117.                 vertexBuffer.Unlock();
  118.                 vertex = null;
  119.             }
  120.         }
  121.  
  122.         public void Render( Matrix worldViewProjection, float alpha, float scale )
  123.         {
  124.             Matrix invertedWorldViewProjection = Matrix.Invert(worldViewProjection);
  125.             effect.SetValue( "invertedWorldViewProjection", invertedWorldViewProjection );
  126.  
  127.             if ((scale == 0.0f) || (alpha == 0.0f)) return; // do nothing if no intensity...
  128.  
  129.             // Draw the skybox
  130.             int pass, numberPasses;
  131.             effect.Technique = "Skybox";
  132.             effect.SetValue( "alpha", alpha );
  133.             effect.SetValue( "scale", alpha * scale );
  134.  
  135.             if(drawSH)
  136.             {
  137.                 effect.SetValue( "environmentTexture", environmentMapSH );
  138.             } 
  139.             else 
  140.             {
  141.                 effect.SetValue( "environmentTexture", environmentMap );
  142.             }
  143.  
  144.             device.SetStreamSource( 0, vertexBuffer, 0, Marshal.SizeOf(typeof(SkyboxVertexFormat)) );
  145.             device.VertexDeclaration = vertexDeclaration;
  146.  
  147.             numberPasses = effect.Begin(0);
  148.             for( pass = 0; pass < numberPasses; pass++ )
  149.             {
  150.                 effect.BeginPass( pass );
  151.                 device.DrawPrimitives( PrimitiveType.TriangleStrip, 0, 2 );
  152.                 effect.EndPass();
  153.             }
  154.             effect.End();
  155.         }
  156.  
  157.         public void OnLostDevice()
  158.         {
  159.             if( effect != null)
  160.                 effect.OnLostDevice();
  161.  
  162.             if(vertexBuffer != null)
  163.             {
  164.                 vertexBuffer.Dispose();
  165.                 vertexBuffer = null;
  166.             }
  167.         }
  168.  
  169.         public void OnDestroyDevice()
  170.         {
  171.             if(environmentMap != null)
  172.             {
  173.                 environmentMap.Dispose();
  174.                 environmentMap = null;
  175.             }
  176.  
  177.             if(environmentMapSH != null)
  178.             {
  179.                 environmentMapSH.Dispose();
  180.                 environmentMapSH = null;
  181.             }
  182.  
  183.             if(effect != null)
  184.             {
  185.                 effect.Dispose();
  186.                 effect = null;
  187.             }
  188.  
  189.             if(vertexDeclaration != null)
  190.             {
  191.                 vertexDeclaration.Dispose();
  192.                 vertexDeclaration = null;
  193.             }
  194.         }
  195.     }
  196. }
  197.